﻿using BlockeyEssentials;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using BlockeyEssentials.ScreenSystem.ScreenTypes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Blockey.MenuSystemScreens
{
    public class MainMenuScreen : MenuScreen
    {
        public MainMenuScreen(ScreenManager oManager)
            : base(oManager, "Main Menu")
        {
            for (MenuItem oOption = MenuItem.Play; oOption <= MenuItem.View; oOption++)
            {
                _Options.Add(oOption.ToString().ToUpper());
            }
            _LastIndex = MenuItem.Create;

            int oWidth = 700;
            int oBuffer = 60;
            int oInsideBuffer = 20;

            _TextFont = new Font(All.Font_Debug);
            _TextFont.Color = Color.Black;
            _TextFont.Layer = 95;

            var oBounds = new Rectangle(
                All.Resolution.Center.X - (oWidth / 2) - oInsideBuffer,
                oBuffer,
                oWidth,
                All.Resolution.Height - (oBuffer * 2));

            var oShape = new Rectangle(
                oBounds.Left + oInsideBuffer,
                oBounds.Top + oInsideBuffer,
                (oBounds.Width - oInsideBuffer) / 2,
                oBounds.Height - (oInsideBuffer * 2));

            //-------- Play Button --------//
            _PlayButton = new MenuButton(oShape, Color.Red, 80, _TextFont, "Play");

            //-------- Create Button --------//
            oShape.X = oShape.Right + oInsideBuffer;
            oShape.Height = (oShape.Height - (oInsideBuffer * 2)) / 3;
            _CreateButton = new MenuButton(oShape, Color.Blue, 80, _TextFont, "Create");

            //-------- Adjust Button --------//
            oShape.Y = oShape.Bottom + oInsideBuffer;
            _AdjustButton = new MenuButton(oShape, Color.Violet, 80, _TextFont, "Adjust");

            //-------- View Button --------//
            oShape.Y = oShape.Bottom + oInsideBuffer;
            _ViewButton = new MenuButton(oShape, Color.Green, 80, _TextFont, "View");

            _SelectedItemSprite = new SpriteR(All.Texture_Square, _PlayButton.Bounds);
            _SelectedItemSprite.Color = Color.Yellow;
            _SelectedItemSprite.Layer = 90;
        }
        
        #region Methods

        public override void Update()
        {
            if (Active)
            {
                Input();
            }
        }

        public override void Draw(SpriteBatch oSB)
        {
            _PlayButton.Draw(oSB, Transition);
            _AdjustButton.Draw(oSB, Transition);
            _CreateButton.Draw(oSB, Transition);
            _ViewButton.Draw(oSB, Transition);
            _SelectedItemSprite.Draw(oSB, Transition);
        }

        protected override void Select()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.Play:
                    {
                        var oScreen = new PlayMenuScreen(_Manager);
                        GoTo(oScreen);
                        break;
                    }
                case MenuItem.Create:
                    {
                        var oScreen = new CreateMenuScreen(_Manager);
                        GoTo(oScreen);
                        break;
                    }
                case MenuItem.Adjust:
                    {
                        var oScreen = new AdjustMenuScreen(_Manager);
                        GoTo(oScreen);
                        break;
                    }
                case MenuItem.View:
                    {
                        var oScreen = new ViewMenuScreen(_Manager);
                        GoTo(oScreen);
                        break;
                    }
            }
        }

        protected override void IndexTo(int oIndex)
        {
            SetLastSelected();
            base.IndexTo(oIndex);
            ChangeSelectedHighlight();
        }

        #region Helpers

        private void Input()
        {
            var oUp = false;
            var oDown = false;
            var oLeftRight = false;
            var oSelect = false;

            var oKeyboardInput = InputManager.Keyboard;
            if (oKeyboardInput.JustPressed(Keys.Up))
                oUp = true;
            if (oKeyboardInput.JustPressed(Keys.Down))
                oDown = true;
            if (oKeyboardInput.JustPressed(Keys.Right, Keys.Left))
                oLeftRight = true;
            if(oKeyboardInput.JustPressed(Keys.Enter, Keys.F))
                oSelect = true;

            var oGamePadInput = InputManager.GamePad1;
            if (oGamePadInput.JustPressed(Buttons.DPadUp))
                oUp = true;
            if (oGamePadInput.JustPressed(Buttons.DPadDown))
                oDown = true;
            if (oGamePadInput.JustPressed(Buttons.DPadLeft, Buttons.DPadRight))
                oLeftRight = true;
            if (oGamePadInput.JustPressed(Buttons.A))
                oSelect = true;

            if (oUp)
                IndexDown();

            if (oDown)
                IndexUp();

            if (oLeftRight)
            {
                var oIndex = (MenuItem)_Index;

                if (oIndex == MenuItem.Play)
                    IndexTo((int)_LastIndex);
                else
                    IndexTo((int)MenuItem.Play);
            }

            if (oSelect)
            {
                Select();
            }

        }

        private void SetLastSelected()
        {
            var oIndex = (MenuItem)_Index;
            if (oIndex != MenuItem.Play)
                _LastIndex = oIndex;
        }

        private void ChangeSelectedHighlight()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.Play: _SelectedItemSprite.Rectangle = _PlayButton.Bounds;
                    break;
                case MenuItem.Create: _SelectedItemSprite.Rectangle = _CreateButton.Bounds;
                    break;
                case MenuItem.Adjust: _SelectedItemSprite.Rectangle = _AdjustButton.Bounds;
                    break;
                case MenuItem.View: _SelectedItemSprite.Rectangle = _ViewButton.Bounds;
                    break;
            }
        }

        #endregion

        #endregion

        private MenuButton _PlayButton;
        private MenuButton _CreateButton;
        private MenuButton _AdjustButton;
        private MenuButton _ViewButton;
        private SpriteR _SelectedItemSprite;
        private Font _TextFont;

        private MenuItem _LastIndex;

        public enum MenuItem
        {
            Play,
            Create,
            Adjust,
            View
        }

    }
}
